package cn.com.haopy.dataes.db.datasource;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@EnableTransactionManagement
@Configuration
@MapperScan(basePackages = "cn.com.haopy.dataes.db.mapper.erp", sqlSessionFactoryRef = "erpSqlSessionFactory")
public class ErpSourceConfig {


    @Autowired
    @Qualifier("mybatisPlusInterceptorOracle")
    private MybatisPlusInterceptor mybatisPlusInterceptorOracle;


    @Bean(name = "erpDatasource")
    @ConfigurationProperties(prefix = "spring.datasource.druid.erp")
    public DataSource erpDatasource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "erpTransactionManager")
    public DataSourceTransactionManager erpTransactionManager(@Qualifier("erpDatasource") DataSource erpDatasource) {
        return new DataSourceTransactionManager(erpDatasource);
    }

    @Bean(name = "erpSqlSessionTemplate")
    public SqlSessionTemplate erpSqlSessionTemplate(@Qualifier("erpSqlSessionFactory") SqlSessionFactory erpSqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(erpSqlSessionFactory);
    }


    @Bean(name = "erpSqlSessionFactory")
    public SqlSessionFactory erpSqlSessionFactory(@Qualifier("erpDatasource") DataSource erpDatasource) throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        //bean.setDataSource(multipleDataSource(hosDatasource(), erpDatasource()));
        //bean.setDataSource(hosDatasource());
        bean.setDataSource(erpDatasource);

        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        bean.setMapperLocations(resolver.getResources("classpath*:mapper/erp/**Mapper.xml"));

        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setCacheEnabled(false);
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setLazyLoadingEnabled(true);
//        configuration.setMultipleResultSetsEnabled(true);
//        configuration.setDefaultStatementTimeout(60);
//        configuration.setDefaultFetchSize(100);
//        configuration.setAutoMappingBehavior(AutoMappingBehavior.FULL);
//        configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);


        bean.setConfiguration(configuration);
        bean.setPlugins(mybatisPlusInterceptorOracle);

        GlobalConfig globalConfig = new GlobalConfig();

        GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
        dbConfig.setIdType(IdType.AUTO);
        dbConfig.setLogicNotDeleteValue("0");
        dbConfig.setLogicDeleteValue("1");
        dbConfig.setLogicDeleteField("deleted");

        globalConfig.setDbConfig(dbConfig);

        bean.setGlobalConfig(globalConfig);

        return bean.getObject();
    }


}
